﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace xO
{
    

    public class AI:Player
    {
        string opponentMarker;

        int movePosition = -1;
        int? score = null;

        public AI(string m):base(m)
        {
            if (marker == "X")
                opponentMarker = "O";
            opponentMarker = "X";
        }

        public override void Move(Game gameinstance, int move)
        {
            
            MaximizedMove(gameinstance, ref movePosition, ref score);

            
            gameinstance.Mark(this.marker, movePosition);
        }

        public void MaximizedMove(Game gameinstance, ref int movePosition, ref int? score)
        {
            int bestMove = -1;
            int? bestScore = null;

            foreach (int m in gameinstance.GetFreePositions())
	        {
		        gameinstance.Mark(this.marker, m);
                
                if (gameinstance.IsGameover())
                {
                    score = GetScore(gameinstance);
                }
                else
                {
                    MinimizedMove(gameinstance, ref movePosition, ref score);
                }

                gameinstance.RevertLastMove(m);
                
                if (bestScore == null || score > bestScore)
                {
                    bestScore = score;
                    bestMove = m;
                }
	        }
            movePosition = bestMove;
            score = bestScore;

            
        }

        public void MinimizedMove(Game gameinstance, ref int move_position, ref int? score)
        {
            int bestmove = -1;
            int? bestscore = null;
            foreach (int m in gameinstance.GetFreePositions())
            {
                gameinstance.Mark(this.opponentMarker, m);
                if (gameinstance.IsGameover())
                {
                    score = GetScore(gameinstance);
                }
                else
                {
                    MaximizedMove(gameinstance, ref move_position, ref score);
                }

                gameinstance.RevertLastMove(m);

                if (bestscore == null || score < bestscore)
                {
                    bestscore = score;
                    bestmove = m;
                    
                }

                
            }
            move_position = bestmove;
            score = bestscore;

            
        }

        public int GetScore(Game gameinstance)
        {
            if (gameinstance.IsGameover())
	        {
                if (gameinstance.Winner == (Winner)Enum.Parse(typeof(Winner), this.marker))
                {
                    return 1;
                }
                else
                {
                    if (gameinstance.Winner == (Winner)Enum.Parse(typeof(Winner), this.opponentMarker))
                    {
                        return -1;
                    } 
                }
	        }
            return 0;
        }
    }
}
